package com.jmpt.fangbohui.ui;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.bean.NewHouseDetailBean.Room;
import com.jmpt.fangbohui.ui.base.BaseHeadActivity;
import com.jmpt.fangbohui.ui.holder.RoomItemHolder;

public class RoomsListActivity extends BaseHeadActivity {
	
	public static final String ROOMS_DATE = "rooms_date";
	
	private ListView mLv;
	
	private List<Room> roomList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		initData();
		
		setContentView(R.layout.activity_rooms_list);
		
		initHeadView();
		
		mLv = (ListView) findViewById(R.id.lv_rooms_list);
		
		mLv.setAdapter(new RoomsAdapter());
		
	}

	private void initData() {
		try {
			String json = getIntent().getStringExtra(ROOMS_DATE);
			roomList = new Gson().fromJson(json, new TypeToken<List<Room>>() {
			}.getType());
		} catch (Exception e) {
			finish();
			e.printStackTrace();
		}
	}
	
	private void initHeadView() {
		// 设置标题
		showTitle("楼层信息");
		// 设置返回键
		showBackButton(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	public class RoomsAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			if(roomList != null) return roomList.size();
			return 0;
		}

		@Override
		public Object getItem(int position) {
			if(roomList != null) return roomList.get(position);
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			RoomItemHolder itemHolder;
			if(convertView == null){
				itemHolder = new RoomItemHolder(mContext);
				convertView = itemHolder.getRootView();
			}else{
				itemHolder = (RoomItemHolder) convertView.getTag();
			}
			itemHolder.setData(roomList.get(position));
			return convertView;
		}
		
	}

}
